package com.hngy.xpq.android_practice.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.bean.Orders;
import com.hngy.xpq.android_practice.database.SQLiteOrder;
import com.hngy.xpq.android_practice.merchant_main.Comm.CommMerchantActivity;

import java.util.List;

public class OrderAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<Orders> list;
    CommMerchantActivity cma;
    SQLiteOrder sqLiteOrder;

    public OrderAdapter(Context context, List<Orders> list) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 :list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        OrderAdapter.ViewHolder viewHolder;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.item_order,null);
            viewHolder = new OrderAdapter.ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (OrderAdapter.ViewHolder) convertView.getTag();
        }
        final Orders o = (Orders) getItem(position);
        //将byte型的图片转换成drawable型
        byte[] c = o.getImage();
        Bitmap b = BitmapFactory.decodeByteArray(c,0,c.length);
        BitmapDrawable bd = new BitmapDrawable(b);

        viewHolder.item_order_image.setImageDrawable(bd);
        viewHolder.item_order_comm.setText(o.getComm_name());
        viewHolder.item_order_name.setText(o.getMerchant_name());
        viewHolder.item_order_price.setText(o.getComm_price());
        viewHolder.item_order_status.setText(o.getStatus());
        viewHolder.item_order_des.setText(o.getDes());
        return convertView;

    }

    class ViewHolder{
        ImageView item_order_image;
        TextView item_order_name,item_order_status,item_order_comm,item_order_price,item_order_des;
        public ViewHolder(View v){
            item_order_image = v.findViewById(R.id.item_order_image);
            item_order_name = v.findViewById(R.id.item_order_name);
            item_order_status = v.findViewById(R.id.item_order_status);
            item_order_comm = v.findViewById(R.id.item_order_comm);
            item_order_price = v.findViewById(R.id.item_order_price);
            item_order_des = v.findViewById(R.id.item_order_des);
        }
    }
}
